﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ZombieMonkeys.Vibria.Logic;
using System.Xml.Linq;
using System.IO.IsolatedStorage;
using System.IO;

namespace DataLayer
{
    public class PuzzleLoader
    {
        private const string path = @"Assets\SudokunatortronData.xml";

        public Puzzle Load(string Id)
        {
            int i = 0;
            int j = 0;
            List<Square> squares = new List<Square>();
            XDocument doc = new XDocument();
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (file.FileExists(path))
                    {
                        using (IsolatedStorageFileStream stream = file.OpenFile(path, FileMode.Open))
                        {
                            doc = XDocument.Load(stream);
                        }
                    }
                    else
                    {
                        doc = XDocument.Load(path);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There is a issue with loading this file:" + ex.Message);
            }
            string elements = string.Empty;
            foreach (XElement element in doc.Root.Elements())
            {
                if (element.FirstAttribute.Value == Id)
                {
                    elements += element.Value;
                }
            }
            foreach (string value in elements.Split(','))
            {
                Square square = new Square();
                square.SetRow(i);
                square.SetColumn(j);
                square.SetPoint();
                square.SetValue(int.Parse(value));
                squares.Add(square);
                if (j != 8)
                {
                    j++;
                }
                else
                {
                    j = 0;
                    i++;
                }
            }
            return new Puzzle(new Grid(squares));
        }

        public List<XElement> LoadList()
        {
            List<XElement> elements = new List<XElement>();
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (file.FileExists(path))
                    {
                        using (IsolatedStorageFileStream stream = file.OpenFile(path, FileMode.Open))
                        {
                            XDocument doc = XDocument.Load(stream,LoadOptions.None);
                            foreach (XElement element in doc.Root.Elements())
                            {
                                elements.Add(element);
                            }
                        }
                    }
                    else
                    {
                        XDocument doc = XDocument.Load(path);
                        foreach (XElement element in doc.Root.Elements())
                        {
                            elements.Add(element);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was a problem loading all of the files:" + ex.Message);
            }
            return elements;
        }
    }
}
